package ru.alexo.whiskey.util;

/**
 * A utility table providing utility functions for convenient access to note
 * codes.
 * 
 * @author Exaybachay
 */
public class TonesHelper {

    private static String[] mNotesInOctave = new String[] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#",
	    "B" };;

    /**
     * Gets the note code according to the MIDI specs.
     * 
     * @param octave
     *            specifies an octave.
     * @param note
     *            the note name
     * @return int code value.
     */
    public static int getNoteCode(Octave octave, Notes note) {
	return getNoteCode(octave, note, null);
    }

    /**
     * Gets the note code according to the MIDI specs.
     * 
     * @param octave
     *            specifies an octave.
     * @param note
     *            the note name
     * @param alt
     *            possible alteration of the note. <tt>null</tt> means no
     *            alteration.
     * @return int code value.
     */
    public static int getNoteCode(Octave octave, Notes note, Alteration alt) {
	return note.getCode() + (octave.ordinal() * 12) + (alt != null ? alt.getValue() : 0);
    }

    /**
     * Get the C note code for the specified octave
     * 
     * @param octave
     *            octave to get the note for
     * @return note code
     */
    public static int getOctaveRootCode(Octave octave) {
	return (octave.ordinal() * 12) + Notes.C.getCode();
    }

    /**
     * Gets you the string representation of the note code.
     * 
     * @param code
     *            code of the note
     * @return
     */
    public static String getNoteNameByCode(int code) {
	return mNotesInOctave[code % 12];
    }

}
